using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Drawing;
using System.Data.Odbc;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.Windows.Media;

using Newtonsoft.Json.Linq;

using Styx;
using Styx.Common;
using Styx.WoWInternals;

namespace WatchMyToon
{
    public class Fucntions_Unit
    {
        public SQL_Connector SQLConnector = new SQL_Connector();
        
        
        // Check if the player is in a Guild
        public bool isInGuild()
        {
            try
            {
                List<string> GuildList = Lua.GetReturnValues("return IsInGuild()");
                if (GuildList == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - isInGuild Lua Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                return false;
            }           
        }
       
		// Get Character Map Posistion
        public void updateCurrentPosition()
        {  
            try
            {
                Lua.DoString("SetMapToCurrentZone();");
                List<string> XY = Lua.GetReturnValues("return GetPlayerMapPosition(\"player\")");
                List<string> currentZone = Lua.GetReturnValues("return GetCurrentMapDungeonLevel()");
                if (StyxWoW.Me.IsInInstance == true)
                {
                    currentZone[0] = Convert.ToString((Convert.ToInt32(currentZone[0]) + 1));
                }

                float _x;
                float.TryParse(XY[0], NumberStyles.Number, CultureInfo.CreateSpecificCulture("en-US"), out _x);

                float _y;
                float.TryParse(XY[1], NumberStyles.Number, CultureInfo.CreateSpecificCulture("en-US"), out _y);

                int x = (((int)Math.Round((_x * 100), 0)) * 3);
                int y = (((int)Math.Round((_y * 100), 0)) * 3);

                try                            
                {
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsDetail\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"szZoneId\":\"" + StyxWoW.Me.ZoneId.ToString() + "\"},{\"nZoneLevelNr\":\"" + currentZone[0] + "\"},{\"szSubZone\":\"" + StyxWoW.Me.SubZoneText + "\"},{\"szZoneText\":\"" + StyxWoW.Me.ZoneText + "\"},{\"nXposition\":\"" + x + "\"},{\"nYposition\":\"" + y + "\"}]", "{\"Query\":\"Update\"}", "{\"Limit\":\"0\"}");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Update Current Position Report: " + sCommandCheck);
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - updateCurrentPosition SQL Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
                           
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - updateCurrentPosition LUA Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
               
            }
        }
        
        // Get the Freebagslots and totalamount
        public string getTotalBagCount()
        {
            int bagtotalcount = 0;
            try
            {
                // get the Freeslots
                for (int i = 0; i < 5; i++)
                {                    
                    List<string> bagTotalCount = Lua.GetReturnValues("return GetContainerNumSlots(" + i + ")");		        
			        bagtotalcount += Convert.ToInt32(bagTotalCount[0].ToString());
                }
                return bagtotalcount.ToString();
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - getBagCount!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                return bagtotalcount.ToString();
            }
        }
     
        // GetClassSpec and so on
        public string getSpec()
        {           
            string returnstring = "";
            try
            {	
                List<string> CurrentSpec = Lua.GetReturnValues("return GetSpecialization()");
                
                if (CurrentSpec[0] != null)
                {
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - CurrentSpec = " + CurrentSpec[0]);
                    List<string> CurrentSpecName = Lua.GetReturnValues("return GetSpecializationInfo(" + CurrentSpec[0] + ")");
                    Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - CurrentSpecName = " + CurrentSpecName[1]);

                    returnstring = CurrentSpecName[1];
                }
                else
                {
                    returnstring = "Unknown";
                }

                return returnstring;
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - getSpec!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                return returnstring;
            }
        }

        // updateGuildInfo Method
		public void updateGuildInfo()
		{
            //Updating Character Guild Information
            if (isInGuild())
            {
                try
                {
                    List<string> Offline = Lua.GetReturnValues("return SetGuildRosterShowOffline(false)");
                    List<string> QueryGuild = Lua.GetReturnValues("return QueryGuildXP()");
                }
                catch (Exception ex)
                {
                    Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - updateGuildInfo LUA Failed!");
                    Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
                }
            }
		}

        // updateTokens Method
        public void updateTokens()
        {
            try
            {
                foreach (string nTokenID in WatchMyToon.arrTokens)
                {
                    List<string> Currency = Lua.GetReturnValues("return GetCurrencyInfo(" + nTokenID + ")");
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABToonsTokens\"},{\"nToonId\":\"" + WatchMyToon.nToonId + "\"},{\"nTokenID\":\"" + nTokenID + "\"},{\"nTokenQty\":\"" + Currency[1] + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");

                    if (sCommandCheck != "SQL Query Successfull!")
                    {
                        Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Token Update Report: " + sCommandCheck);
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - updateTokens SQL Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }

        // updateReputations Method
        public void updateReputations()
        {
            try
            {                
                List<string> reputationCount = Lua.GetReturnValues("return GetNumFactions()");
                for (int i = 1; i <= Convert.ToInt32(reputationCount[0]); i++)
                {
                    List<string> reputation = Lua.GetReturnValues("return GetFactionInfo(" + i + ")");
                    string sCommandCheck = SQLConnector.SendJSON("[{\"TAB\":\"TABReputation\"},{\"iToonID\":\"" + WatchMyToon.nToonId + "\"},{\"UID\":\"\"},{\"iRepID\":\"" + i + "\"},{\"sName\":\"" + reputation[0] + "\"},{\"sDescription\":\"" + reputation[1] + "\"},{\"iStandingID\":\"" + reputation[2] + "\"},{\"iBottomValue\":\"" + reputation[3] + "\"},{\"iTopValue\":\"" + reputation[4] + "\"},{\"iEarnedValue\":\"" + reputation[5] + "\"},{\"bAtWarWith\":\"" + reputation[6] + "\"},{\"bCanToggleAtWar\":\"" + reputation[7] + "\"},{\"bIsHeader\":\"" + reputation[8] + "\"},{\"bIsCollapsed\":\"" + reputation[9] + "\"},{\"bHasRep\":\"" + reputation[10] + "\"},{\"bIsWatched\":\"" + reputation[11] + "\"},{\"bIsChild\":\"" + reputation[12] + "\"}]", "{\"Query\":\"Insert\"}", "{\"Limit\":\"0\"}");

                    if (sCommandCheck != "SQL Query Successfull!")
                    {
                        Logging.Write(LogLevel.Verbose, Colors.Orange, "[WatchMyToon] - Reputation Update Report: " + sCommandCheck);
                    }
                     
                }
            }
            catch (Exception ex)
            {
                Logging.Write(LogLevel.Normal, Colors.Red, "[WatchMyToon] - updateReputations Lua/SQL Failed!");
                Logging.Write(LogLevel.Diagnostic, Colors.Red, "[WatchMyToon] - " + ex.Message);
            }
        }
        

	}
}
